package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ro.dandanciu.webgallery.domainmodel.utils.DbConstants;

/**
 * Domain model class for adverts.
 * @author Dan
 */
@Entity
@Table(name = "TP_ADVERT")
@NamedQueries({@NamedQuery(name = "Advert.findForUser",
        query = "select a from Advert a left outer join a.users u where u.userId = :userId") })
public class Advert {

    /**
     * The identifier of the advert.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "K_ADVERT")
    private long id;

    /**
     * The text of the advert.
     */
    @Column(name = "C_TEXT", length = DbConstants.Advert.TEXT_SIZE)
    private String aText;

    /**
     * The target gallery. The gallery for which the advert is made.
     */
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "F_TARGET")
    private Gallery gallery;

    /**
     * The group of users to which the advert is sent.
     */
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "TP_ADVERT_CLIENTS",
            joinColumns = { @JoinColumn(name = "FK_ADVERT") },
            inverseJoinColumns = { @JoinColumn(name = "FK_USER") })
    private Set < User > users;

    /**
     * The timestamp of the last time this item was updated. Mapped to
     * D_UPDATED.
     */
    @Column(name = "D_UPDATED")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date updated;

    /**
     * The user id/software which has performed the last update. Mapped to
     * C_UPDATER.
     */
    @Column(name = "C_UPDATER", length = DbConstants.Generic.MODIFIED_SIZE)
    private String updater;

    /**
     * @return the id
     */
    public final long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public final void setId(final long id) {
        this.id = id;
    }

    /**
     * @return the aText
     */
    public final String getAText() {
        return aText;
    }

    /**
     * @param text the aText to set
     */
    public final void setAText(final String text) {
        aText = text;
    }

    /**
     * @return the gallery
     */
    public final Gallery getGallery() {
        return gallery;
    }

    /**
     * @param gallery the gallery to set
     */
    public final void setGallery(final Gallery gallery) {
        this.gallery = gallery;
    }

    /**
     * @return the users
     */
    public final Set < User > getUsers() {
        return users;
    }

    /**
     * @param users the users to set
     */
    public final void setUsers(final Set < User > users) {
        this.users = users;
    }

    /**
     * @return the updated
     */
    public final Date getUpdated() {
        return updated;
    }

    /**
     * @param updated the updated to set
     */
    public final void setUpdated(final Date updated) {
        this.updated = updated;
    }

    /**
     * @return the updater
     */
    public final String getUpdater() {
        return updater;
    }

    /**
     * @param updater the updater to set
     */
    public final void setUpdater(final String updater) {
        this.updater = updater;
    }
}
